#include <sstream>
#include "pokedex.h"
#include "xmlParser.h"


Pokedex& Pokedex::operator=(Pokedex &)
{
  return *this;
}

Pokedex::Pokedex() : QWidget(NULL)
{
	try
	{
		XMLParser gimmePokemons("pokedata.xml");
		this->setupUi(this);
		this->connect(this->listWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem*)), this, SLOT(fillInformations2(QListWidgetItem *, QListWidgetItem *)));
		this->pokelist = gimmePokemons.getPokemons();
		std::map<QString, Pokemon>::iterator itl = this->pokelist.begin();
		std::map<QString, Pokemon>::iterator itle = this->pokelist.end();
		for (; itl != itle; ++itl)
		{
			QString mini("small_img/");
			mini += this->pokelist[(*itl).first].getId();
			mini += ".png";
			this->listWidget->addItem(new QListWidgetItem(QIcon(QString(mini)), (*itl).first));
			this->list->addItem(new QListWidgetItem(QIcon(QString(mini)), (*itl).first));
		}
	}
	catch (std::exception *e)
	{
		std::cout << e->what() << std::endl;
	}
}

Pokedex::~Pokedex()
{
}


void	Pokedex::aff_pic(int id)
{
	std::ostringstream temp; 
	std::string num;
	temp << id; 
	num = temp.str();
	std::string link = "img/";
	if (id < 10)
		num = "00" + num;
	else if (id < 100)
		num = "0" + num;
	std::string final_link = link + num + ".jpg";
	this->picture->setPixmap(QPixmap(QString::fromUtf8(final_link.c_str())));
}

QString	Pokedex::aff_pic_evo(int id) const
{
	std::ostringstream temp; 
	QString num;
	temp << id; 
	num = temp.str().c_str();
	QString link = "img/";
	if (id < 10)
		num = "00" + num;
	else if (id < 100)
		num = "0" + num;
	QString final_link = link + num + ".jpg";
	return (final_link);
}

const QString Pokedex::mapGetName(const QString &id) const
{
	std::map<QString, Pokemon>::const_iterator itl = this->pokelist.begin();
	std::map<QString, Pokemon>::const_iterator itle = this->pokelist.end();

	for (; itl != itle; ++itl)
	{
		if ((*itl).second.getId() == id)
			return ((*itl).second.getName());
	}
	return (NULL);
}

void Pokedex::eraseAllLabels()
{
	this->Evo_name_2->setText("");
	this->Picture_2->setPixmap(QPixmap(""));
	this->Evo_name_2->setText("");
	this->Evo_level_2->setText("");
	this->Evo_con_2->setText("");
	this->Evo_label_name_2->setText("");
	this->Evo_label_level_2->setText("");
	this->Evo_label_con_2->setText("");

	this->Evo_name_3->setText("");
	this->Picture_3->setPixmap(QPixmap(""));
	this->Evo_name_3->setText("");
	this->Evo_level_3->setText("");
	this->Evo_con_3->setText("");
	this->Evo_label_name_3->setText("");
	this->Evo_label_level_3->setText("");
	this->Evo_label_con_3->setText("");

	this->Evo_name_4->setText("");
	this->Picture_4->setPixmap(QPixmap(""));
	this->Evo_name_4->setText("");
	this->Evo_level_4->setText("");
	this->Evo_con_4->setText("");
	this->Evo_label_name_4->setText("");
	this->Evo_label_level_4->setText("");
	this->Evo_label_con_4->setText("");

	this->Evo_name_5->setText("");
	this->Picture_5->setPixmap(QPixmap(""));
	this->Evo_name_5->setText("");
	this->Evo_level_5->setText("");
	this->Evo_con_5->setText("");
	this->Evo_label_name_5->setText("");
	this->Evo_label_level_5->setText("");
	this->Evo_label_con_5->setText("");

	this->Evo_name_6->setText("");
	this->Picture_6->setPixmap(QPixmap(""));
	this->Evo_name_6->setText("");
	this->Evo_level_6->setText("");
	this->Evo_con_6->setText("");
	this->Evo_label_name_6->setText("");
	this->Evo_label_level_6->setText("");
	this->Evo_label_con_6->setText("");

	this->Evo_name_7->setText("");
	this->Picture_7->setPixmap(QPixmap(""));
	this->Evo_name_7->setText("");
	this->Evo_level_7->setText("");
	this->Evo_con_7->setText("");
	this->Evo_label_name_7->setText("");
	this->Evo_label_level_7->setText("");
	this->Evo_label_con_7->setText("");

	this->Evo_name_8->setText("");
	this->Picture_8->setPixmap(QPixmap(""));
	this->Evo_name_8->setText("");
	this->Evo_level_8->setText("");
	this->Evo_con_8->setText("");
	this->Evo_label_name_8->setText("");
	this->Evo_label_level_8->setText("");
	this->Evo_label_con_8->setText("");
}

void Pokedex::fillInformations2(QListWidgetItem *item, QListWidgetItem *)
{
	fillInformations(item);
}

void Pokedex::eraseLists()
{
	this->List_level->clear();
	this->List_tm->clear();
	this->List_egg->clear();
}

void Pokedex::fillInformations(QListWidgetItem *item)
{
	Pokemon pkmn = this->pokelist[item->text()];

	QString name = pkmn.getName();
	this->Name->setText(name);
	std::list<QString> s = pkmn.getType();
	QString Type;
	Type = (*(s.begin()));
	if (s.size() > 1)
	{
		Type += ", ";
		s.pop_front();
		Type += (*(s.begin()));
	}
	this->Type->setText(Type);
	QString ability = pkmn.getAbility();
	this->Ability->setText(ability);
	QString height = pkmn.getHeight();
	this->Height->setText(height);
	QString weight = pkmn.getWeight();
	this->Weight->setText(weight);
	QString desc = pkmn.getDescription();
	this->Description->setText(desc);
	QString img = pkmn.getId();
	this->aff_pic(img.toInt());
	std::map<QString, QString> ratios = pkmn.getRatio();
	this->Male->setText(ratios["male"]);
	this->Female->setText(ratios["female"]);

	std::map<QString, QString> stats = pkmn.getStats();
	this->ATK_bar->setValue(stats["ATK"].toInt());
	this->ATK_val->setText(stats["ATK"]);
	this->HP_bar->setValue(stats["HP"].toInt());
	this->HP_val->setText(stats["HP"]);
	this->DEF_bar->setValue(stats["DEF"].toInt());
	this->DEF_val->setText(stats["DEF"]);
	this->SDP_bar->setValue(stats["SPD"].toInt());
	this->SPD_val->setText(stats["SPD"]);
	this->SAT_bar->setValue(stats["SAT"].toInt());
	this->SAT_val->setText(stats["SAT"]);
	this->SDF_bar->setValue(stats["SDF"].toInt());
	this->SDF_val->setText(stats["SDF"]);

	std::map<QString, std::pair<QString, QString> > evo = pkmn.getEvolution();

	std::map<QString, std::pair<QString, QString> >::iterator evoit = evo.begin();
	std::map<QString, std::pair<QString, QString> >::iterator evoite = evo.end();

	QString imgpath = this->aff_pic_evo(((*evoit).first).toInt());
	this->eraseAllLabels();
	this->Evo_name_1->setText(imgpath);
	this->Picture_1->setPixmap(QPixmap(QString(imgpath)));
	this->Evo_name_1->setText(mapGetName((*evoit).first));
	this->Evo_level_1->setText((*evoit).second.first);
	this->Evo_con_1->setText((*evoit).second.second);
	this->Evo_label_name_1->setText("Name:");
	this->Evo_label_level_1->setText("Level:");
	this->Evo_label_con_1->setText("Condition:");
	++evoit;
	if (evoit != evoite)
	{
		QString imgpath2 = this->aff_pic_evo(((*evoit).first).toInt());
		this->Evo_name_2->setText(imgpath2);
		this->Picture_2->setPixmap(QPixmap(QString(imgpath2)));
		this->Evo_name_2->setText(mapGetName((*evoit).first));
		this->Evo_level_2->setText((*evoit).second.first);
		this->Evo_con_2->setText((*evoit).second.second);
		this->Evo_label_name_2->setText("Name:");
		this->Evo_label_level_2->setText("Level:");
		this->Evo_label_con_2->setText("Condition:");
		++evoit;
		if (evoit != evoite)
		{
			QString imgpath3 = this->aff_pic_evo(((*evoit).first).toInt());
			this->Evo_name_3->setText(imgpath3);
			this->Picture_3->setPixmap(QPixmap(QString(imgpath3)));
			this->Evo_name_3->setText(mapGetName((*evoit).first));
			this->Evo_level_3->setText((*evoit).second.first);
			this->Evo_con_3->setText((*evoit).second.second);
			this->Evo_label_name_3->setText("Name:");
			this->Evo_label_level_3->setText("Level:");
			this->Evo_label_con_3->setText("Condition:");
			++evoit;
			if (evoit != evoite)
			{
				QString imgpath4 = this->aff_pic_evo(((*evoit).first).toInt());
				this->Evo_name_4->setText(imgpath4);
				this->Picture_4->setPixmap(QPixmap(QString(imgpath4)));
				this->Evo_name_4->setText(mapGetName((*evoit).first));
				this->Evo_level_4->setText((*evoit).second.first);
				this->Evo_con_4->setText((*evoit).second.second);
				this->Evo_label_name_4->setText("Name:");
				this->Evo_label_level_4->setText("Level:");
				this->Evo_label_con_4->setText("Condition:");
				++evoit;
				if (evoit != evoite)
				{
					QString imgpath5 = this->aff_pic_evo(((*evoit).first).toInt());
					this->Evo_name_5->setText(imgpath5);
					this->Picture_5->setPixmap(QPixmap(QString(imgpath5)));
					this->Evo_name_5->setText(mapGetName((*evoit).first));
					this->Evo_level_5->setText((*evoit).second.first);
					this->Evo_con_5->setText((*evoit).second.second);
					this->Evo_label_name_5->setText("Name:");
					this->Evo_label_level_5->setText("Level:");
					this->Evo_label_con_5->setText("Condition:");
					++evoit;
					if (evoit != evoite)
					{
						QString imgpath6 = this->aff_pic_evo(((*evoit).first).toInt());
						this->Evo_name_6->setText(imgpath6);
						this->Picture_6->setPixmap(QPixmap(QString(imgpath6)));
						this->Evo_name_6->setText(mapGetName((*evoit).first));
						this->Evo_level_6->setText((*evoit).second.first);
						this->Evo_con_6->setText((*evoit).second.second);
						this->Evo_label_name_6->setText("Name:");
						this->Evo_label_level_6->setText("Level:");
						this->Evo_label_con_6->setText("Condition:");
						++evoit;
						if (evoit != evoite)
						{
							QString imgpath7 = this->aff_pic_evo(((*evoit).first).toInt());
							this->Evo_name_7->setText(imgpath7);
							this->Picture_7->setPixmap(QPixmap(QString(imgpath7)));
							this->Evo_name_7->setText(mapGetName((*evoit).first));
							this->Evo_level_7->setText((*evoit).second.first);
							this->Evo_con_7->setText((*evoit).second.second);
							this->Evo_label_name_7->setText("Name:");
							this->Evo_label_level_7->setText("Level:");
							this->Evo_label_con_7->setText("Condition:");
							++evoit;
							if (evoit != evoite)
							{
								QString imgpath8 = this->aff_pic_evo(((*evoit).first).toInt());
								this->Evo_name_8->setText(imgpath8);
								this->Picture_8->setPixmap(QPixmap(QString(imgpath8)));
								this->Evo_name_8->setText(mapGetName((*evoit).first));
								this->Evo_level_8->setText((*evoit).second.first);
								this->Evo_con_8->setText((*evoit).second.second);
								this->Evo_label_name_8->setText("Name:");
								this->Evo_label_level_8->setText("Level:");
								this->Evo_label_con_8->setText("Condition:");
							}
						}
					}
				}
			}
		}
	}
	eraseLists();
	std::map<QString, QString> lvl_up = pkmn.getLvlup();
	std::map<QString, QString>::iterator itl = lvl_up.begin();
	std::map<QString, QString>::iterator itle = lvl_up.end();
	for (; itl != itle; ++itl)
	{
		this->List_level->addItem((*itl).first + " - level " + (*itl).second);
	}

	std::map<QString, QString> hm = pkmn.getHM();
	std::map<QString, QString>::iterator itl2 = hm.begin();
	std::map<QString, QString>::iterator itle2 = hm.end();
	for (; itl2 != itle2; ++itl2)
	{
		this->List_tm->addItem((*itl2).first + " - level " + (*itl2).second);
	}

	std::map<QString, QString> tm = pkmn.getTM();
	std::map<QString, QString>::iterator itl3 = tm.begin();
	std::map<QString, QString>::iterator itle3 = tm.end();
	for (; itl3 != itle3; ++itl3)
	{
		this->List_tm->addItem((*itl3).first + " - level " + (*itl3).second);
	}

	std::list<QString> egg = pkmn.getEgg();
	std::list<QString>::iterator itl4 = egg.begin();
	std::list<QString>::iterator itle4 = egg.end();
	for (; itl4 != itle4; ++itl4)
	{
		this->List_egg->addItem((*itl4));
	}
}
